package com.jwm.platform;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class MessageFilter implements Filter {
    @Resource
    private PlatformConfig platformConfig;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        if (httpServletRequest.getRequestURI().indexOf("/jwm/") == 0) {
            chain.doFilter(request, response);
            return;
        }

        String authorization = ((HttpServletRequest) request).getHeader("Authorization");
        boolean contains = platformConfig.getAuthList().contains(authorization);
        if (contains) {
            chain.doFilter(request, response);
            return;
        }
        request.getRequestDispatcher("/error").forward(request, response);
    }
}
